import { course as CourseApi } from '../../../../../config/request.js'
import {
    resources
} from '../../../../../util/';

var checkClassTime = (rule, value, callback) => {
    if (!value) {
        return callback(new Error('请选择上课时间范围'));
    }
    setTimeout(() => {
        if (!value[0] || !value[1]) {
            callback(new Error('请选择上课时间范围'));
        } else {
            callback();
        }
    }, 0);
};
var checkReleaseTime = (rule, value, callback) => {
    if (!value) {
        return callback(new Error('请选择课程发布时间'));
    }
    setTimeout(() => {
        callback();
    }, 0);
};
module.exports = {
    name: 'class',
    data() {
        return {
            tableData: [],
            currentPage: 1,
            tableTotal: 10,
            loading: false,
            tableCount: resources.tableCount,
            layout: resources.layout,
            updateFlag: false,
            previewVisible: false,
            previewData: {},
            classForm: {
                title: '',
                author: {
                    info: '',
                    describe: ''
                },
                video_url: '',
                cover_url: '',
                need_integral: 0,
                video_length: '',
                remark: '',
                class_time: [],
                release_time: ''
            },
            formVisible: false,
            rules: {
                title: [
                    { type: "string", required: true, message: '课程名称', trigger: 'blur' }
                ],
                info: [
                    { required: false }
                ],
                describe: [
                    { required: false }
                ],
                video_url: [
                    { type: "string", required: true, message: '视频地址', trigger: 'blur' }
                ],
                cover_url: [
                    { type: "string", required: true, message: '封面地址', trigger: 'blur' }
                ],
                video_length: [
                    { required: false }
                ],
                need_integral: [
                    { required: false }
                ],
                remark: [
                    { required: false }
                ],
                class_time: [
                    { required: true, validator: checkClassTime, trigger: 'blur' }
                ],
                release_time: [
                    { required: true, validator: checkReleaseTime, trigger: 'blur' }
                ],
            },
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            fields: {
                title: {
                    info: {
                        prop: 'title',
                        label: '课程名称',
                        sortable: true
                    },
                    filter: {
                        multiple: false
                    },
                    style: {
                        width: '120',
                        align: 'center'
                    }
                },
                author: {
                    info: {
                        prop: 'author',
                        label: '讲课老师',
                        sortable: true
                    },
                    filter: {
                        multiple: false
                    },
                    style: {
                        width: '120',
                        align: 'center'
                    }
                },
                status: {
                    info: {
                        prop: 'status',
                        label: '课程状态',
                        sortable: true
                    },
                    filter: {
                        list: resources.classStatus,
                        multiple: false
                    },
                    style: {
                        width: '120',
                        align: 'center'
                    }
                },
                need_integral: {
                    info: {
                        prop: 'need_integral',
                        label: '报名积分',
                        sortable: true
                    },
                    filter: {
                        multiple: false
                    },
                    style: {
                        width: '120',
                        align: 'center'
                    }
                },
                cover_url: {
                    info: {
                        prop: 'cover_url',
                        label: '封面url',
                        sortable: true
                    },
                    filter: {
                        multiple: false
                    },
                    style: {
                        width: '120',
                        align: 'center'
                    }
                },
                video_url: {
                    info: {
                        prop: 'video_url',
                        label: '视频url',
                        sortable: true
                    },
                    filter: {
                        multiple: false
                    },
                    style: {
                        width: '120',
                        align: 'center'
                    }
                },
                start_time: {
                    info: {
                        prop: 'start_time',
                        label: '课程开始时间',
                        sortable: true
                    },
                    filter: {
                        multiple: false
                    },
                    style: {
                        width: '150',
                        align: 'center'
                    }
                },
                end_time: {
                    info: {
                        prop: 'end_time',
                        label: '课程结束时间',
                        sortable: true
                    },
                    filter: {
                        multiple: false
                    },
                    style: {
                        width: '120',
                        align: 'center'
                    }
                },
                release_time: {
                    info: {
                        prop: 'release_time',
                        label: '课程发布时间',
                        sortable: true
                    },
                    filter: {
                        multiple: false
                    },
                    style: {
                        width: '120',
                        align: 'center'
                    }
                },
            }
        }
    },
    methods: {
        formatterStatus(item) {
            for (let i in resources.classStatus) {
                if (item.status == resources.classStatus[i].value) {
                    return resources.classStatus[i].label
                }
            }
        },
        filterStatus(value, item) {
            return value == item.status;
        },
        formatterStime(item) {
            return this._formatterTime(item.start_time)
        },
        formatterEtime(item) {
            return this._formatterTime(item.end_time)
        },
        formatterRtime(item) {
            return this._formatterTime(item.release_time)
        },
        _formatterTime(time) {
            return resources.formatterDetailTime(time);
        },
        formatterAuthor(item) {
            var author = item.author;
            if (author) {
                try {
                    var authorObj = JSON.parse(author);
                    return authorObj.info;
                } catch (err) {
                    return author;
                }
            }
            return '';
        },
        checkLevel() {
            var user = this.$store.state.user.userinfo;
            // console.log('user', user);
            return user && user.level == 1;
        },
        onEditClass(course) {
            this.classForm = {
                cid: course.cid,
                title: course.title,
                author: JSON.parse(course.author),
                video_url: course.video_url,
                cover_url: course.cover_url,
                need_integral: course.need_integral,
                remark: course.remark,
                class_time: [new Date(course.start_time * 1000), new Date(course.end_time * 1000)],
                release_time: new Date(course.release_time * 1000),
                video_length: course.video_length
            };
            if (this.$refs['classForm']) {
                this.$refs['classForm'].resetFields();
            }
            this.updateFlag = true;
            this.formVisible = true;
        },
        onPreviewClass(course) {
            this.previewData = course;
            this.previewVisible = true;
        },
        onPostClass(course) {
            this.$confirm('是否确认发布该课程？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this._postCourse(course);
            }).catch((err) => {
                this.$message.error(err);
            });
        },
        onDeleteClass(course) {
            this.$confirm('是否确认删除该课程？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this._deleteCourse(course);
            }).catch((err) => {
                this.$message.error(err);
            });
        },
        onAddClass() {
            this.classForm = {
                title: '',
                author: {
                    info: '',
                    describe: ''
                },
                video_url: '',
                cover_url: '',
                need_integral: 0,
                remark: '',
                class_time: [],
                release_time: '',
                video_length: ''
            }
            if (this.$refs['classForm']) {
                this.$refs['classForm'].resetFields();
            }
            this.updateFlag = false;
            this.formVisible = true;
        },
        save(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.updateFlag) {
                        this._updateCourse();
                    } else {
                        this._addCourse();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        _addCourse() {
            var course = {
                title: this.classForm.title,
                author: JSON.stringify(this.classForm.author),
                video_url: this.classForm.title,
                cover_url: this.classForm.title,
                start_time: new Date(this.classForm.class_time[0]).getTime() / 1000,
                end_time: new Date(this.classForm.class_time[1]).getTime() / 1000,
                release_time: new Date(this.classForm.release_time).getTime() / 1000,
                need_integral: this.classForm.need_integral,
                video_length: this.classForm.video_length,
                remark: this.classForm.remark
            }
            CourseApi.addCourse.call(this, course, data => {
                this.tableData.splice(0, 0, data['course']);
                this.$message.success('创建课程信息成功 ！！');
                this.formVisible = false;
            })
        },
        _updateCourse() {
            var course = {
                cid: this.classForm.cid,
                title: this.classForm.title,
                author: JSON.stringify(this.classForm.author),
                video_url: this.classForm.video_url,
                cover_url: this.classForm.cover_url,
                start_time: new Date(this.classForm.class_time[0]).getTime() / 1000,
                end_time: new Date(this.classForm.class_time[1]).getTime() / 1000,
                release_time: new Date(this.classForm.release_time).getTime() / 1000,
                need_integral: this.classForm.need_integral,
                video_length: this.classForm.video_length,
                remark: this.classForm.remark
            }
            CourseApi.updateCourse.call(this, course, data => {
                for (let i = this.tableData.length - 1; i >= 0; i--) {
                    if (this.tableData[i].cid == data['course'].cid) {
                        this.tableData.splice(i, 1, data['course']);
                        this.$message.success('修改课程信息成功 ！！');
                        this.formVisible = false;
                        return;
                    }
                }
            })
        },
        _deleteCourse(course) {
            CourseApi.deleteCourse.call(this, { cid: course.cid }, data => {
                this.$message.success('删除课程信息成功 ！！');
                this.tableData.splice(this.tableData.indexOf(course), 1)
            })
        },
        _postCourse(course) {
            CourseApi.postCourse.call(this, { cid: course.cid }, data => {
                for (let i = this.tableData.length - 1; i >= 0; i--) {
                    if (this.tableData[i].cid == data['course'].cid) {
                        this.tableData[i].status = data['course'].status;
                        this.$message.success('课程发布成功 ！！');
                        return;
                    }
                }
            })
        },
        _getCourse() {
            CourseApi.getCourses.call(this, { page: this.currentPage }, data => {
                console.log(data)
                this.tableData = data['lit'];
                this.tableTotal = data['count'];
                this.loading = false;
            })
        }
    },
    created: function() {
        this._getCourse();
    }

}